# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import execjs
import os


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
抖音-signature生成。网页版
参考:https://github.com/coder-fly/douyin-signature
+++++++++++++++++++++++++++++++++++++++++++++++++
# 查看当前js环境
print(execjs.get().name)
# 设置execjs执行Node
os.environ["EXECJS_RUNTIME"] = "Node"
# 读解密脚本
with open("douyin.js", "r", encoding="gbk") as f:
    line = f.read()
# ctx = execjs.compile(line)  # node
ctx = execjs.compile(line,cwd=r'D:\Program Files\nodejs\node_global\node_modules')  # node jsdom
sign1 = ctx.call('generateSignature', data['tac'], data['user_id'])
+++++++++++++++++++++++++++++++++++++++++++++++++
"""
"""
根据抖音id获取用户作品信息。点赞、分享
"""

headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "accept": "application/json",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9"
}

response = requests.get('https://www.iesdouyin.com/share/user/58958068057',headers=headers,verify=False)

tac = re.search(r"tac='([\s\S]*?)'</script>",response.text).group(1)
data = {
    "tac":tac.split("|")[0],
    'user_id':'58958068057'
}
# 查看当前js环境
print(execjs.get().name)
# 设置execjs执行Node
os.environ["EXECJS_RUNTIME"] = "Node"
# 读解密脚本
with open("douyin.js", "r", encoding="gbk") as f:
    line = f.read()
# ctx = execjs.compile(line)  # node
ctx = execjs.compile(line,cwd=r'D:\Program Files\nodejs\node_global\node_modules')  # node jsdom
sign1 = ctx.call('generateSignature', data['tac'], data['user_id'])
# 从服务器获sign
response = requests.post('http://49.233.200.77:5001',data=data)
sign = response.json().get('signature')
print(sign1)
print(sign)

uids = '''60227614217'''
proxies = {
    # 'https':'https://' + '180.105.100.172:30111'
}
max_cursor = 0
while True:
    response = requests.post('http://49.233.200.77:5001',data=data)
    sign = response.json().get('signature')
    url = "https://www.iesdouyin.com/web/api/v2/aweme/post/?user_id={}&sec_uid=&count=21&max_cursor={}&aid=1128&_signature={}"
    url = url.format(uids.strip(), max_cursor, sign)
    response = requests.get(url,headers=headers,verify=False,proxies=proxies)
    data_json = response.json()
    for d in data_json['aweme_list']:
        dict_data = dict()
        dict_data['aweme_id'] = d['aweme_id']
        dict_data['desc'] = d['desc']  # 简介
        dict_data['share_count'] = d['statistics']['share_count']  # 分享数量
        dict_data['forward_count'] = d['statistics']['forward_count']
        dict_data['comment_count'] = d['statistics']['comment_count']  # 评论数量
        dict_data['digg_count'] = d['statistics']['digg_count']  # 点赞数量
        dict_data['duration'] = d['video']['duration']  # 视频时长
        dict_data['share_video'] = d['video']['dynamic_cover']['list_url'][0]  # 视频分享链接
        print(dict_data)


    if data_json['has_more'] == False:
        break
    max_cursor = data_json['max_cursor']

